*&---------------------------------------------------------------------*
*& 包含               ZFIU019_CLS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.
  PUBLIC SECTION.
* Handle datachanged
    METHODS:HANDLE_DATA_CHANGED
      FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
      IMPORTING ER_DATA_CHANGED.

    METHODS HANDLE_MODIFY
      FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID
      IMPORTING E_MODIFIED ET_GOOD_CELLS.

*--Hotspot click control
    METHODS:HANDLE_HOTSPOT_CLICK               " 审批
      FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
      IMPORTING
        E_ROW_ID
        E_COLUMN_ID
        ES_ROW_NO.

    METHODS:
      HANDLE_DOUBLE_CLICK FOR EVENT DOUBLE_CLICK
        OF CL_GUI_ALV_GRID
        IMPORTING E_ROW
                  E_COLUMN
                  ES_ROW_NO.

    METHODS:
      HANDLE_F4 FOR EVENT ONF4 OF CL_GUI_ALV_GRID
        IMPORTING E_FIELDNAME
                  E_FIELDVALUE
                  ES_ROW_NO
                  ER_EVENT_DATA
                  ET_BAD_CELLS
                  E_DISPLAY.


ENDCLASS.                    "lcl_event_receiver DEFINITION

CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

  "EDIT ALV的回车事件
  METHOD HANDLE_DATA_CHANGED.

    DATA:
      LV_CHG_FLAG TYPE C,
      LS_LVC_MODI TYPE LVC_S_MODI,
      LV_MWSKZ    TYPE T007S-MWSKZ,
      LV_SAKNR    TYPE SKB1-SAKNR,
      LV_KOSTL    TYPE CSKS-KOSTL,
      LV_TAXRATE  TYPE ZFIT079_HEAD-ZFPSL,
      LV_TAXEXIST TYPE C,
      LV_SHUZI    TYPE ZFIT079_HEAD-ZBHSJE,
      LV_DATE     TYPE SY-DATUM,
      LV_MSG      TYPE STRING.

    CLEAR GV_ERROR.

    ER_DATA_CHANGED->REFRESH_PROTOCOL( ).

    "修改内表的值
    LOOP AT ER_DATA_CHANGED->MT_MOD_CELLS INTO LS_LVC_MODI. "WHERE VALUE IS NOT INITIAL.
      IF LS_LVC_MODI-VALUE IS NOT INITIAL.
        CONDENSE LS_LVC_MODI-VALUE.

        "数据检查
        IF LS_LVC_MODI-FIELDNAME = 'WRBTR' OR LS_LVC_MODI-FIELDNAME = 'GL_WRBTR' OR LS_LVC_MODI-FIELDNAME = 'ERFMG'.
          "是否为数字检查并报错
          REPLACE ALL OCCURRENCES OF ',' IN LS_LVC_MODI-VALUE WITH ''.
          TRY .
              LV_SHUZI  = LS_LVC_MODI-VALUE.
            CATCH CX_SY_CONVERSION_NO_NUMBER.

              GV_ERROR = 'X'.
              CLEAR LV_MSG.
              CONCATENATE TEXT-M11 LS_LVC_MODI-VALUE TEXT-M12 INTO LV_MSG SEPARATED BY SPACE.
              PERFORM FRM_ADD_PROTOCOL_ENTRY USING '00' 'E' '001' LS_LVC_MODI-FIELDNAME
                                                 LV_MSG '' LS_LVC_MODI-ROW_ID ER_DATA_CHANGED.
          ENDTRY.

        ELSEIF LS_LVC_MODI-FIELDNAME = 'MWSKZ' OR LS_LVC_MODI-FIELDNAME = 'GL_MWSKZ'.
          LV_MWSKZ = LS_LVC_MODI-VALUE.

          "检查税码是否存在
          "根据税率取税码
          PERFORM FRM_GET_TAX_RATE USING 'CN' LV_MWSKZ CHANGING LV_TAXRATE LV_TAXEXIST.
          IF LV_TAXEXIST IS INITIAL.

            GV_ERROR = 'X'.
            CLEAR LV_MSG.
            LV_MSG = TEXT-M14.
            REPLACE ALL OCCURRENCES OF '&' IN LV_MSG WITH LV_MWSKZ.
            PERFORM FRM_ADD_PROTOCOL_ENTRY USING '00' 'E' '001' LS_LVC_MODI-FIELDNAME
                                                 LV_MSG '' LS_LVC_MODI-ROW_ID ER_DATA_CHANGED.
          ENDIF.

        ELSEIF LS_LVC_MODI-FIELDNAME = 'GL_HKONT'.
          LV_SAKNR = LS_LVC_MODI-VALUE.
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
            EXPORTING
              INPUT  = LV_SAKNR
            IMPORTING
              OUTPUT = LV_SAKNR.

          SELECT
            COUNT(*)
            FROM SKB1
           WHERE BUKRS = ZFIT079_HEAD-BUKRS
             AND SAKNR = LV_SAKNR.

          IF SY-SUBRC <> 0.
            GV_ERROR = 'X'.
            CLEAR LV_MSG.
            LV_MSG = TEXT-M15.
            REPLACE ALL OCCURRENCES OF '&' IN LV_MSG WITH LV_SAKNR.
            PERFORM FRM_ADD_PROTOCOL_ENTRY USING '00' 'E' '001' LS_LVC_MODI-FIELDNAME
                                                 LV_MSG '' LS_LVC_MODI-ROW_ID ER_DATA_CHANGED.
          ENDIF.

        ELSEIF LS_LVC_MODI-FIELDNAME = 'GL_KOSTL'.
          LV_KOSTL = LS_LVC_MODI-VALUE.
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
            EXPORTING
              INPUT  = LV_KOSTL
            IMPORTING
              OUTPUT = LV_KOSTL.

          SELECT
            COUNT(*)
            FROM CSKS
           WHERE KOKRS = '1000'
             AND KOSTL = LV_KOSTL
             AND DATBI >= SY-DATUM.

          IF SY-SUBRC <> 0.
            GV_ERROR = 'X'.
            CLEAR LV_MSG.
            LV_MSG = TEXT-M16.
            REPLACE ALL OCCURRENCES OF '&' IN LV_MSG WITH LV_KOSTL.
            PERFORM FRM_ADD_PROTOCOL_ENTRY USING '00' 'E' '001' LS_LVC_MODI-FIELDNAME
                                                 LV_MSG '' LS_LVC_MODI-ROW_ID ER_DATA_CHANGED.
          ENDIF.

        ENDIF.
      ENDIF.


      "数据调整
      IF GV_ERROR IS INITIAL.
        READ TABLE GT_ALV INTO DATA(LS_ALV) INDEX LS_LVC_MODI-ROW_ID.
        IF SY-SUBRC = 0.
          LV_CHG_FLAG = 'X'.

          CASE LS_LVC_MODI-FIELDNAME.
            WHEN 'SELECT'.
              LS_ALV-SELECT = LS_LVC_MODI-VALUE.

            WHEN 'ERFMG'.
              LS_ALV-ERFMG = LS_LVC_MODI-VALUE.

            WHEN 'WRBTR'.
              LS_ALV-WRBTR = LS_LVC_MODI-VALUE.
              IF LS_ALV-WRBTR IS INITIAL.

                IF LS_ALV-PEINH IS NOT INITIAL.
                  LS_ALV-WRBTR = LS_ALV-ERFMG * LS_ALV-NETPR / LS_ALV-PEINH. "本次开票不含税总价(行项目)
                ELSE.
                  LS_ALV-WRBTR = LS_ALV-ERFMG * LS_ALV-NETPR.
                ENDIF.
              ENDIF.

              LS_ALV-ZFPSE = ( LS_ALV-WRBTR + LS_ALV-BEZNK ) * LS_ALV-ZFPSL / 100.

            WHEN 'MWSKZ'.
              LS_ALV-MWSKZ = LS_LVC_MODI-VALUE.
              LS_ALV-ZFPSL = LV_TAXRATE.
              LS_ALV-ZFPSE = ( LS_ALV-WRBTR + LS_ALV-BEZNK ) * LS_ALV-ZFPSL / 100.


            WHEN 'GL_HKONT'.
              LS_ALV-GL_HKONT = LV_SAKNR.
              LS_ALV-SELECT = 'X'.
              LS_ALV-GL_SHKZG = 'S'.
              SELECT SINGLE
                TXT20
                INTO LS_ALV-GL_HKONT_TXT
                FROM SKAT
               WHERE SPRAS = SY-LANGU
                 AND KTOPL = GC_KTOPL_VAMA
                 AND SAKNR = LS_ALV-GL_HKONT.

            WHEN 'GL_KOSTL'.
              LS_ALV-GL_KOSTL = LV_KOSTL.
              SELECT SINGLE
                KTEXT
                INTO LS_ALV-GL_KOSTL_TXT
                FROM CSKT
               WHERE SPRAS = SY-LANGU
                 AND KOKRS = GC_KOKRS_1000
                 AND KOSTL = LS_ALV-GL_KOSTL
                 AND DATBI >= SY-DATUM.

            WHEN 'GL_SHKZG'.
              LS_ALV-GL_SHKZG = LS_LVC_MODI-VALUE.

            WHEN 'GL_SGTXT'.
              LS_ALV-GL_SGTXT = LS_LVC_MODI-VALUE.

            WHEN 'GL_WRBTR'.
              LS_ALV-GL_WRBTR = LS_LVC_MODI-VALUE.

              LS_ALV-GL_ZFPSE = LS_ALV-GL_WRBTR * LS_ALV-GL_ZFPSL / 100.

            WHEN 'GL_MWSKZ'.
              LS_ALV-GL_MWSKZ = LS_LVC_MODI-VALUE.
              LS_ALV-GL_ZFPSL = LV_TAXRATE.
              LS_ALV-GL_ZFPSE = LS_ALV-GL_WRBTR * LS_ALV-GL_ZFPSL / 100.

            WHEN OTHERS.
          ENDCASE.

          MODIFY GT_ALV FROM LS_ALV INDEX LS_LVC_MODI-ROW_ID.
        ENDIF.
      ENDIF.
    ENDLOOP.
    IF SY-SUBRC = 0 AND GV_ERROR IS INITIAL AND LV_CHG_FLAG IS NOT INITIAL. "有修改
      PERFORM FRM_CACULATE_ITEM.

      IF SAVE_OK IS INITIAL. "ALV 屏幕回车刷新
        LEAVE TO SCREEN SY-DYNNR.
      ENDIF.
    ENDIF.

  ENDMETHOD.                    "CATCH_DOUBLECLICK

  "Detail ALV的回车事件
  METHOD HANDLE_MODIFY.


  ENDMETHOD.

  METHOD HANDLE_HOTSPOT_CLICK.

    ME->HANDLE_DOUBLE_CLICK(
      EXPORTING
        ES_ROW_NO = ES_ROW_NO
        E_COLUMN  = E_COLUMN_ID
    ).

  ENDMETHOD.                    "HANDLE_HOTSPOT_CLICK

  METHOD HANDLE_DOUBLE_CLICK.
    DATA:
      LV_TCODE      TYPE SY-TCODE,
      LS_PARAMETER  TYPE /PTGWFI/PIDVAL,
      LT_PARAMETERS TYPE TABLE OF /PTGWFI/PIDVAL,
      LV_MSG1(80)   TYPE C,
      LV_MSG2(80)   TYPE C.

    READ TABLE GT_ALV INTO DATA(LS_ALV) INDEX ES_ROW_NO-ROW_ID.

    CASE E_COLUMN.
      WHEN 'EBELN'.
        IF LS_ALV-EBELN IS NOT INITIAL.

          LV_TCODE = 'ME23N'.
          MOVE 'BES'      TO LS_PARAMETER-PID.
          MOVE LS_ALV-EBELN   TO LS_PARAMETER-VALUE.
          APPEND LS_PARAMETER TO LT_PARAMETERS.

          CALL FUNCTION '/PTGWFI/CP_CALLTRANSACTION'
            EXPORTING
              TRAN_CODE             = LV_TCODE
            TABLES
              PID_VAL               = LT_PARAMETERS
            EXCEPTIONS
*             OTHERS                = 1.
* Start of CHG VIMI-18450
              SYSTEM_FAILURE        = 1 MESSAGE LV_MSG1
              COMMUNICATION_FAILURE = 2
              OTHERS                = 3.
          IF SY-SUBRC EQ 1 .
            MESSAGE LV_MSG1 TYPE 'S' DISPLAY LIKE 'E'.

          ELSEIF SY-SUBRC EQ 2 .
            MESSAGE LV_MSG2 TYPE 'S' DISPLAY LIKE 'E'.

          ELSEIF SY-SUBRC = 3.
            MESSAGE I076(/OPT/VIM_M_CLASS).
          ENDIF.
        ENDIF.

      WHEN 'MBLNR' OR 'MJAHR' OR 'ZEILE'.
        IF LS_ALV-MBLNR IS NOT INITIAL AND LS_ALV-MJAHR IS NOT INITIAL.
          SET PARAMETER ID 'MBN' FIELD LS_ALV-MBLNR.
          SET PARAMETER ID 'MJA' FIELD LS_ALV-MJAHR.
          CALL FUNCTION 'MIGO_DIALOG'
            EXPORTING
              I_ACTION            = 'A04'
              I_REFDOC            = 'R02'
              I_NOTREE            = 'X'
              I_SKIP_FIRST_SCREEN = 'X'
              I_DEADEND           = 'X'
              I_MBLNR             = LS_ALV-MBLNR
              I_MJAHR             = LS_ALV-MJAHR.
        ENDIF.
      WHEN OTHERS.
    ENDCASE.

    CLEAR LS_ALV.
  ENDMETHOD.


  METHOD HANDLE_F4.

  ENDMETHOD.
ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION
